﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
using System.Linq;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Action.Service;
using Newtonsoft.Json;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.SPI;
using Newtonsoft.Json.Linq;

namespace T.FAS.Runtime.Expression.Plugin
{
    public class ActionExecutor : DynamicObject, IDynamicActionAdapter
    {
        /// <summary>
        /// JS脚本动态调用Dynamic必备函数
        /// 脚本写法：Action.CreateCDO_123(TRecord);
        /// </summary>
        /// <param name="name">动态调用DynamicObject中的函数名称</param>
        /// <param name="args">参数信息，参数类型必须增加 params</param>
        /// <returns></returns>
        public object DynamicInvoke(string name, params object[] args)
        {
            try
            {
                return ExcuteAction(name, args);
            }
            catch(Exception e)
            {
                Base.Common.LogUtil.Log(JsonConvert.SerializeObject(e.ToString()), "ExprError");
                throw;
            }
        }

        public dynamic ExcuteAction(string actionID, object[] paramList)
        {
            List<ActionParameter> parameters = new List<ActionParameter>();
            for (int i = 0; i < paramList.Length; i++)
            {
                parameters.Add(new ActionParameter { Order = i, ParameterValue = paramList[i] });
            }
            TResult result = ActionExecuteService.GetInstance.Execute(actionID, ActionType.Action, parameters,true);
            if(result is TCommonResult<JObject>)
            {
                return result as TCommonResult<JObject>;
            }
            else
            {
                return result as TCommonResult<TRecord>;
            }
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            result = DynamicInvoke(binder.Name, args);
            return true;
        }
        public List<object> Parallel(Dictionary<string, List<object>> list, string actionID, Dictionary<string, object> paramList)
        {
            List<object> results = new List<object>();
            list.AsParallel().ForAll(t => {
                List<ActionParameter> parameters = new List<ActionParameter>();
                foreach (var item in paramList.Keys)
                {
                    parameters.Add(new ActionParameter { ParameterName = item, ParameterValue = paramList[item] });
                }
                TResult result = ActionExecuteService.GetInstance.Execute(actionID, ActionType.Action, parameters, false);
                if (result is TCommonResult<JObject>)
                {

                    results.Add(result as TCommonResult<JObject>);
                }
                else
                {
                    results.Add(result as TCommonResult<TRecord>);
                }
            });
            return results;
        }
        public void Parallel(List<ActionParalleEntity> list, string Expr,Dictionary<string,object> inParam)
        {
            list.AsParallel().ForAll(t => {
            });
        }
    }
    public class ActionParalleEntity
    {
        public IList<object> ParalleDatas { get; set; }
        public Dictionary<string, object> inParams { get; set; }
    }
}
